﻿Imports System.Data.SqlClient
Public Class frmHangHoaDs

    Private Sub frmHangHoaDs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UpdateList()
    End Sub

    Private Sub dgvList_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvList.CellDoubleClick
        Dim frm As New frmHangHoa(Me.dgvList.CurrentRow.Cells(1).Value)
        frm.ShowDialog()
        If frm.booThanhCong = True Then
            UpdateList()
        End If
    End Sub

    Private Sub UpdateList()
        Dim strSql As String = "select ROW_NUMBER() OVER (ORDER BY MaHH) as STT, MaHH as [Mã hàng hóa], TenHH as [Tên hàng hóa], DonViTinh as [Đơn vị tính], DonGiaXuat as [Đơn giá xuất], TonDK_SL as [Tồn ĐK(SL)], TonDK_GT as [Tồn ĐK(GT)], TonCK_SL as [Tồn CK(SL)], TonCK_GT as [Tồn CK(GT)] from HangHoa order by STT"
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myTable As New DataTable
        Try
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(strSql, myConn)
            myAdapter.Fill(myTable)
            dgvList.DataSource = myTable
            dgvList.Columns.Item(0).Width = 50
            dgvList.Columns.Item(1).Width = 100
            dgvList.Columns.Item(2).Width = 300
            'dgvList.Columns.Item(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        Catch ex As Exception

        End Try
        myConn.Close()
        myConn.Dispose()
    End Sub

    Private Function DeleteData(ByVal MaHH As String) As Boolean
        Dim strSql As String = "Delete from HangHoa where MaHH = @MaHH"
        Dim myConn As New SqlConnection(modKetNoi.strConnectionString)
        Dim myComm As New SqlCommand
        Dim result As Boolean = True
        Try
            myConn.Open()
            myComm.Connection = myConn
            myComm.CommandText = strSql
            myComm.Parameters.AddWithValue("@MaHH", MaHH)
            myComm.ExecuteNonQuery()
        Catch ex As Exception
            result = False
        End Try
        myConn.Close()
        myConn.Dispose()
        Return result
    End Function

    Private Sub dgvList_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvList.KeyDown
        If e.KeyCode = Keys.Delete Then
            If Me.dgvList.SelectedRows.Count > 0 Then
                If MsgBox("Xóa Hàng hóa đã chọn?", MsgBoxStyle.OkCancel, "Xác nhận") = MsgBoxResult.Ok Then
                    If DeleteData(Me.dgvList.SelectedRows.Item(0).Cells(1).Value) Then
                        UpdateList()
                    Else
                        MsgBox("Lỗi xóa dữ liệu!")
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub NewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripButton.Click
        Dim frm As New frmHangHoa()
        frm.ShowDialog()
        If frm.booThanhCong = True Then
            UpdateList()
        End If
    End Sub

    Private Sub XoaToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoaToolStripButton.Click
        If Me.dgvList.SelectedRows.Count > 0 Then
            If MsgBox("Xóa Hàng hóa đã chọn?", MsgBoxStyle.OkCancel, "Xác nhận") = MsgBoxResult.Ok Then
                If DeleteData(Me.dgvList.SelectedRows.Item(0).Cells(1).Value) Then
                    UpdateList()
                Else
                    MsgBox("Lỗi xóa dữ liệu!")
                End If
            End If
        Else
            MsgBox("Vui lòng chọn dòng cần Xóa!")
        End If
    End Sub

    Private Sub SuaToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SuaToolStripButton.Click
        If Me.dgvList.SelectedRows.Count > 0 Then
            Dim frm As New frmHangHoa(Me.dgvList.SelectedRows.Item(0).Cells(1).Value)
            frm.ShowDialog()
            If frm.booThanhCong = True Then
                UpdateList()
            End If
        Else
            MsgBox("Vui lòng chọn dòng cần Sửa!")
        End If
    End Sub

    Private Sub DongToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DongToolStripButton.Click
        Me.Close()
    End Sub
End Class